<template>
  <div class="alarmRecord">
    <DFPageList ref="dfPageListRef" :page-options="pageOptions" />
  </div>
</template>

<script lang="ts" setup name="AlarmRecord">
  import lang from './lang';
  import { pageAlarmRecord, exportAlarmRecord } from '@/api/wip/alarmRecord';
  import { useMixins } from '@/hooks/useMixins';
  import { listShutdownReason } from '@/api/base/shutdownReason';
  import { listMachine } from '@/api/base/machine';
  import { listShift } from '@/api/base/shift';
  import { dateFn } from '@/utils/http/axios/ipcIndex';

  const { loadLangFn } = useMixins();

  const dfPageListRef: any = ref(null);

  loadLangFn(lang);

  const machineList = ref([]);

  // const materialList = ref([]);

  const shiftList = ref([]);

  const shutdownReasonList = ref([]);

  const pageOptions = {
    searchForm: {
      formSchemas: [
        {
          label: 'machineName',
          prop: 'machineCode',
          component: 'NSelect',
          options: machineList,
        },
      ],
    },
    table: {
      hasCreate: false,
      columns: [
        {
          label: 'alarmRecordCode',
        },
        {
          label: 'alarmStatusName',
        },
        {
          label: 'machineCode',
        },
        {
          label: 'machineName',
        },
        {
          label: 'alarmTypeName',
        },
        {
          label: 'alarmArea',
        },
        {
          label: 'alarmCode',
        },
        {
          label: 'alarmName',
        },
        {
          label: 'occurTime',
          render: (row) => {
            return h('span', {
              innerText: row.occurTime ? dateFn(new Date(row.occurTime)) : '-',
            });
          },
        },
        {
          label: 'closeTime',
          render: (row) => {
            return h('span', {
              innerText: row.closeTime ? dateFn(new Date(row.closeTime)) : '-',
            });
          },
        },
      ],
      operaColumn: [
        {
          label: 'export',
        },
      ],
      requestFn: pageAlarmRecord,
      exportRequestFn: exportAlarmRecord,
    },
  };

  async function initDictData() {
    const res = await Promise.all([listShift(), listMachine(), listShutdownReason()]);

    const [res1, res2, res3] = res;

    shiftList.value =
      res1?.data.map((item) => ({
        label: item.shiftName,
        value: item.shiftCode,
        userList: item.userList,
      })) || [];

    machineList.value =
      res2?.data.map((item) => ({ label: item.machineName, value: item.machineCode })) || [];

    shutdownReasonList.value =
      res3?.data.map((item) => ({
        label: `${item.shutdownReasonName}`,
        value: item.shutdownReasonCode,
      })) || [];
  }

  initDictData();

  // nextTick(() => {
  //   dfPageListRef.value && dfPageListRef.value.openModalFn();
  // });
</script>
